From 4369b25867a0a05aa0b1e89d3c3ab1e942b4daeb Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Tue, 28 Jun 2011 12:19:15 +0100 Subject: [PATCH] xl: new global -N option for dry run This sets a global dryrun_only variable, which individual commands are expected to honour. To avoid accidents, we introduce a new can_dryrun member in the command table, which is initially set to 0 for each command. Signed-off-by: Ian Jackson Committed-by: Ian Jackson --- tools/libxl/xl.c | 17 ++++-- tools/libxl/xl.h | 2 + tools/libxl/xl_cmdimpl.c | 5 +- tools/libxl/xl_cmdtable.c | 124 +++++++++++++++++++------------------- 4 files changed, 80 insertions(+), 68 deletions(-) diff --git a/tools/libxl/xl.c b/tools/libxl/xl.c index 5bab2ff1b8..1f231a83c2 100644 --- a/tools/libxl/xl.c +++ b/tools/libxl/xl.c @@ -34,6 +34,7 @@ #include "xl.h" xentoollog_logger_stdiostream *logger; +int dryrun_only; int autoballoon = 1; char *lockfile; char *default_vifscript = NULL; @@ -90,11 +91,14 @@ int main(int argc, char **argv) void *config_data = 0; int config_len = 0; - while ((opt = getopt(argc, argv, "+v")) >= 0) { + while ((opt = getopt(argc, argv, "+vN")) >= 0) { switch (opt) { case 'v': if (minmsglevel > 0) minmsglevel--; break; + case 'N': + dryrun_only = 1; + break; default: fprintf(stderr, "unknown global option\n"); exit(2); @@ -138,9 +142,14 @@ int main(int argc, char **argv) optind = 1; cspec = cmdtable_lookup(cmd); - if (cspec) + if (cspec) { + if (dryrun_only && !cspec->can_dryrun) { + fprintf(stderr, "command does not implement -N (dryrun) option\n"); + ret = 1; + goto xit; + } ret = cspec->cmd_impl(argc, argv); - else if (!strcmp(cmd, "help")) { + } else if (!strcmp(cmd, "help")) { help(argv[1]); ret = 0; } else { @@ -148,8 +157,8 @@ int main(int argc, char **argv) ret = 1; } + xit: libxl_ctx_free(ctx); xtl_logger_destroy((xentoollog_logger*)logger); - return ret; } diff --git a/tools/libxl/xl.h b/tools/libxl/xl.h index bce135abb0..04f9045ff0 100644 --- a/tools/libxl/xl.h +++ b/tools/libxl/xl.h @@ -20,6 +20,7 @@ struct cmd_spec { char *cmd_name; int (*cmd_impl)(int argc, char **argv); + int can_dryrun; char *cmd_desc; char *cmd_usage; char *cmd_option; @@ -103,6 +104,7 @@ extern xentoollog_logger_stdiostream *logger; /* global options */ extern int autoballoon; +extern int dryrun_only; extern char *lockfile; extern char *default_vifscript; diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c index 0850162803..217af4f976 100644 --- a/tools/libxl/xl_cmdimpl.c +++ b/tools/libxl/xl_cmdimpl.c @@ -1674,7 +1674,7 @@ void help(const char *command) struct cmd_spec *cmd; if (!command || !strcmp(command, "help")) { - printf("Usage xl [-v] [args]\n\n"); + printf("Usage xl [-vN] [args]\n\n"); printf("xl full list of subcommands:\n\n"); for (i = 0; i < cmdtable_len; i++) printf(" %-20s%s\n", @@ -1682,7 +1682,8 @@ void help(const char *command) } else { cmd = cmdtable_lookup(command); if (cmd) { - printf("Usage: xl [-v] %s %s\n\n%s.\n\n", + printf("Usage: xl [-v%s] %s %s\n\n%s.\n\n", + cmd->can_dryrun ? "N" : "", cmd->cmd_name, cmd->cmd_usage, cmd->cmd_desc); diff --git a/tools/libxl/xl_cmdtable.c b/tools/libxl/xl_cmdtable.c index a89980315a..5308288b53 100644 --- a/tools/libxl/xl_cmdtable.c +++ b/tools/libxl/xl_cmdtable.c @@ -19,7 +19,7 @@ struct cmd_spec cmd_table[] = { { "create", - &main_create, + &main_create, 0, "Create a domain from config file ", " [options] [vars]", "-h Print this help.\n" @@ -32,7 +32,7 @@ struct cmd_spec cmd_table[] = { "-e Do not wait in the background for the death of the domain." }, { "list", - &main_list, + &main_list, 0, "List information about all/some domains", "[options] [Domain]\n", "-l, --long Output all VM details\n" @@ -40,59 +40,59 @@ struct cmd_spec cmd_table[] = { "-Z, --context Prints out security context" }, { "destroy", - &main_destroy, + &main_destroy, 0, "Terminate a domain immediately", "", }, { "shutdown", - &main_shutdown, + &main_shutdown, 0, "Issue a shutdown signal to a domain", "", }, { "reboot", - &main_reboot, + &main_reboot, 0, "Issue a reboot signal to a domain", "", }, { "pci-attach", - &main_pciattach, + &main_pciattach, 0, "Insert a new pass-through pci device", " [Virtual Slot]", }, { "pci-detach", - &main_pcidetach, + &main_pcidetach, 0, "Remove a domain's pass-through pci device", " ", }, { "pci-list", - &main_pcilist, + &main_pcilist, 0, "List pass-through pci devices for a domain", "", }, { "pci-list-assignable-devices", - &main_pcilist_assignable, + &main_pcilist_assignable, 0, "List all the assignable pci devices", "", }, { "pause", - &main_pause, + &main_pause, 0, "Pause execution of a domain", "", }, { "unpause", - &main_unpause, + &main_unpause, 0, "Unpause a paused domain", "", }, { "console", - &main_console, + &main_console, 0, "Attach to domain's console", "[options] \n" "-t console type, pv or serial\n" "-n console number" }, { "vncviewer", - &main_vncviewer, + &main_vncviewer, 0, "Attach to domain's VNC server.", "[options] \n" "--autopass Pass VNC password to viewer via stdin and\n" @@ -100,14 +100,14 @@ struct cmd_spec cmd_table[] = { "--vncviewer-autopass (consistency alias for --autopass)" }, { "save", - &main_save, + &main_save, 0, "Save a domain state to restore later", "[options] []", "-h Print this help.\n" "-c Leave domain running after creating the snapshot." }, { "migrate", - &main_migrate, + &main_migrate, 0, "Save a domain state to restore later", "[options] ", "-h Print this help.\n" @@ -119,12 +119,12 @@ struct cmd_spec cmd_table[] = { " of the domain." }, { "dump-core", - &main_dump_core, + &main_dump_core, 0, "Core dump a domain", " " }, { "restore", - &main_restore, + &main_restore, 0, "Restore a domain from a saved state", "[options] [] ", "-h Print this help.\n" @@ -133,63 +133,63 @@ struct cmd_spec cmd_table[] = { "-d Enable debug messages." }, { "migrate-receive", - &main_migrate_receive, + &main_migrate_receive, 0, "Restore a domain from a saved state", "- for internal use only", }, { "cd-insert", - &main_cd_insert, + &main_cd_insert, 0, "Insert a cdrom into a guest's cd drive", " ", }, { "cd-eject", - &main_cd_eject, + &main_cd_eject, 0, "Eject a cdrom from a guest's cd drive", " ", }, { "mem-max", - &main_memmax, + &main_memmax, 0, "Set the maximum amount reservation for a domain", " ", }, { "mem-set", - &main_memset, + &main_memset, 0, "Set the current memory usage for a domain", " ", }, { "button-press", - &main_button_press, + &main_button_press, 0, "Indicate an ACPI button press to the domain", "